package 华为OD机试B卷复用题.基础题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class HJ32_最长的顺子 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试B卷复用题/基础题/input/32.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    static Map<String, Integer> index =
            new HashMap<String, Integer>() {
                {
                    put("3", 0);
                    put("4", 1);
                    put("5", 2);
                    put("6", 3);
                    put("7", 4);
                    put("8", 5);
                    put("9", 6);
                    put("10", 7);
                    put("J", 8);
                    put("Q", 9);
                    put("K", 10);
                    put("A", 11);
                }
            };

    static void Solution(Scanner in) {
        String[] s1Array = in.nextLine().split("-");
        String[] s2Array = in.nextLine().split("-");
        int[] cnt = new int[12];
        for (int i = 0; i < s1Array.length; i++) {
            cnt[index.get(s1Array[i])]++;
        }
        for (int i = 0; i < s2Array.length; i++) {
            cnt[index.get(s2Array[i])]++;
        }
        int start = 0, tmpNum = 0, maxNum = 0;
        for (int i = 0; i < 12; i++) {
            if (cnt[i] < 4) {
                tmpNum++;
            } else {
                if (tmpNum >= 5 && tmpNum >= maxNum) {
                    start = i - tmpNum;
                    maxNum = tmpNum;
                    tmpNum = 0;
                } else {
                    tmpNum = 0;
                }
            }
        }
        if (tmpNum >= 5 && tmpNum >= maxNum) {
            start = 12 - tmpNum;
            maxNum = tmpNum;
            tmpNum = 0;
        } else if (maxNum == 0) {
            System.out.println("NO-CHAIN");
            return;
        }
        String output = "3456789XJQKA";
        String res = output.substring(start, start + maxNum);
        for (int i = 0; i < res.length(); i++) {
            if (i != 0) {
                System.out.print("-");
            }
            char now = res.charAt(i);
            if (now == 'X') {
                System.out.print("10");
                continue;
            }
            System.out.print(now);
        }
    }
}
